package com.minhua.jijiehao2.mainframe.account.channellistactivity.impl

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.ChannelBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.account.addaccountactivity.impl.AddAccountActivity
import com.minhua.jijiehao2.mainframe.account.channellistactivity.imp.IChannelListView
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import kotlinx.android.synthetic.main.activity_channel_list.*

class ChannelListActivity : BaseActivity<IChannelListView, ChannelListPresenter>(), IChannelListView {

    override fun getLayoutId(): Int = R.layout.activity_channel_list

    override fun getPresenter(): ChannelListPresenter = ChannelListPresenter()

    private val listChannel = arrayListOf<ChannelBean>()
    private var adapter: BaseCommonAdapter<ChannelBean>? = null
    private var linearLayoutManager: LinearLayoutManager? = null
    private var lastItemPosition: Int = 0

    private var switch: Int = 0
    override fun initView() {
        switch = intent.getIntExtra(DefineKey.SWITCH, 0)
        adapter = object : BaseCommonAdapter<ChannelBean>(this, R.layout.item_add_channel, listChannel) {
            override fun convert(holder: ViewHolder?, t: ChannelBean?) {
                if (switch == 1) {
                    Glide.with(this@ChannelListActivity).load(t?.picUrl).into(holder?.getView(R.id.ivIcon))
                    holder?.setText(R.id.tvChannelName, t?.pname)
                } else {
                    holder?.setText(R.id.tvChannelName, "集借号" + t?.id + "期")
                }
                holder?.setOnClickListener(R.id.llItemView, {
                    if (LoginActivity.logged(this@ChannelListActivity)) {
                        AddAccountActivity.intentStart(this@ChannelListActivity, t?.pcode!!, if (switch == 1) {
                            t.picUrl
                        } else {
                            ""
                        })
                    }
                })
            }
        }
        linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContent.layoutManager = linearLayoutManager
        rvContent.adapter = adapter

        refresh.setOnRefreshListener { mPresenter.getListChannel() }

        rvContent.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                lastItemPosition = linearLayoutManager!!.findLastCompletelyVisibleItemPosition()
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastItemPosition != 0 && lastItemPosition + 1 == adapter?.itemCount) {
                    mPresenter.loadMoreListChannel()
                }
            }
        })
    }

    override fun initData() {
        mPresenter.getListChannel()
    }


    override fun onChannelListSuccess(list: List<ChannelBean>, needClear: Boolean) {
        refresh.isRefreshing = false
        if (needClear) {
            listChannel.clear()
        }
        if (list.isNotEmpty()) {
            listChannel.addAll(list)
            adapter?.notifyDataSetChanged()
        }
    }
}
